<?php

namespace app\controller;

use app\service\EntityService;
use support\Request;
use support\Response;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;

class EntityController
{


    /**
     * @var EntityService
     */
    private $entityService;

    public function __construct()
    {
        $this->entityService = EntityService::getInstance();
    }


    /**
     * 获取实体的关联统计
     * @param Request $request
     * @return Response
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function getMultipleEntityRelationCount(Request $request): Response
    {
        $param = $request->post();
        $projectId = $param['project_id'];
        $res = $this->entityService->getMultipleEntityRelationCount($projectId, $param);
        return json(success_response('', $res));
    }

    /**
     * 获取资产关联的镜头列表
     * @param Request $request
     * @return Response
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function getAssetRelationShotTaskCardList(Request $request): Response
    {
        $param = $request->post();
        $res = $this->entityService->getAssetRelationShotTaskCardList($param['param']['entity_id']);
        return json(success_response('success', $res));
    }

    /**
     * 获取资产关联的镜头列表
     * @param Request $request
     * @return Response
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function getAssetRelationShotList(Request $request): Response
    {
        $param = $request->post()['param'];
        $entityId = $param['entity_id'] ?? 0;
        $initialEpisodeCode = param_to_array($param['initial_episode_code'] ?? '');

        $res = $this->entityService->getAssetRelationShotList($request, $entityId, $initialEpisodeCode);
        return json(success_response('', $res));
    }

    /**
     * 获取资产关联的镜头统计
     * @param Request $request
     * @return Response
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function getAssetRelationShotCount(Request $request): Response
    {
        $param = $request->post()['param'];
        $entityId = $param['entity_id'] ?? 0;
        $projectId = $param['project_id'] ?? 0;
        $page = param_to_array($param['page'] ?? '', ',', 'intval');

        $res = $this->entityService->getAssetRelationShotCount($request, $entityId, $projectId, $page);
        return json(success_response('', $res));
    }

    /**
     * 获取多个实体的关联ids
     * @param Request $request
     * @return Response
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function getMultipleEntityRelationIds(Request $request): Response
    {

        $param = $request->post();

        $entityIds = param_to_array($param['entity_ids'] ?? '', ',', 'intval');
        $res = $this->entityService->getMultipleEntityRelationIds($request, $entityIds);
        return json(success_response('', $res));
    }
}
